#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
class Solution {
    bool col[10];
    bool dig1[20];
    bool dig2[20];
    vector<string> path;
    vector<vector<string>> ret;
    int a;
public:
    vector<vector<string>> solveNQueens(int n) {
        a = n;
        path.resize(n);
        for (int i = 0; i < n; i++)
        {
            path[i].append(n, '.');
        }
        dfs(0);
        return ret;
    }
    void dfs(int row)
    {
        if (row == a)
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < a; i++)
        {
            if (!col[i] && !dig1[i - row + a] && !dig2[i + row])
            {
                path[row][i] = 'Q';
                col[i] = true;
                dig1[i - row + a] = true;
                dig2[i + row] = true;
                dfs(row + 1);
                col[i] = false;
                dig1[i - row + a] = false;
                dig2[i + row] = false;
                path[row][i] = '.';
            }
        }

    }
};
int main()
{
    Solution as;
    vector<vector<string>> aq=as.solveNQueens(5);
    for (int i = 0; i < aq.size(); i++)
    {
        for (int j = 0; j < aq[i].size(); j++)
        {
            cout<<aq[i][j] <<" ";
        }
        cout << endl;
    }
    return 0;
}